package c.x.jy.service.utils.mqtt;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
@SuppressWarnings("all")
public class CacheReBackMsg {

	private static String cacheType;

	private static MemCacheImpl cache=new MemCacheImpl();
	private static MemCacheImpl memCacheImpl= new MemCacheImpl(); ;
	private static String Key_xf="xf";
	private static String Key_out="out";
	
	   public final static String userkey="userinfo:";
	   public final static String advertsKey="adverts:" ;
	   public final static String CONFIGKEY="configkey:" ;
	   public final static String PAYKEY="PAYKEY:";
	   public final static String ROLEKEY="ROLEKEY:";
	   public final static String MENUKEY="MENUKEY:";
	   public final static String NEXTLEVELLIST="NEXTLEVELLIST:";
	   private final static String defultKey="___";
	   public final static String DEVICEKEY="DEVICEKEY:";
	   public final static String BUYKEY="BUYKEY:"; 
	   public final static String BUYKEYDONE="BUYKEYDONE:"; 
	   public final static String LUCKDRAWKEY="LUCKDRAW:"; 
 
 
	
	public static void put(String key ,Object value){
		cache.setObj(defultKey+key, value, 60*10);
	}
	/**
	 * 
	 * @param key
	 * @param value
	 * @param time 秒
	 */
	public static void put(String key ,String value,int time){
		cache.setObj(defultKey+ key, value, time);
	}
	
	public static Object get(String key){
		return cache.getObj(defultKey+key);
	}
	
	public static String getWEIXINCFG(String o) {
		return cache.get("WEIXINCFG",o);
	}

	public static void setWEIXINCFG(String key, String o) {
		cache.put("WEIXINCFG",key,o,60 * 5);
	}

	//设备在线状态缓存
	public static String getDeviceStatus(String o) {
		return cache.get("deviceStatus",o);
	}

	public static void setDeviceStatus(String key, String o) {
		cache.put("deviceStatus",key,o,5 * 60);
	}


	public static String getWEIXINCONFIG(String o) {
		return cache.get("WEIXINCONFIG",o);
	}

	public static void setWEIXINCONFIG(String key, String o) {
		cache.put("WEIXINCONFIG",key,o,7150);
	}
	
	public static void removeWEIXINCONFIG(String key) {
	
		
		cache.remove("WEIXINCONFIG", key);
	}


	public static String getRebackMsg(String o) {
		return cache.get("rebackMsg",o);
	}
	
	public static String getDeviceBackStatus(String o) {
		return cache.get("DeviceBackStatus",o);
	}
	public static void setDeviceBackStatus(String key, String o) {
		cache.put("DeviceBackStatus",key,o,60);
	}
	/**
	 * 设置缓存信息避免重复出纸统计
	 *
	 * @param key
	 * @param o
	 */
	public static void setRebackMsg(String key, String o) {
		cache.put("rebackMsg",key,o,50);
	}

	public static String getDeviceNetStatuss(String dev_no) {
		return cache.get("deviceNetStatus",dev_no);
	}

	public static void setDeviceNetStatus(String dev_no, String obj) {
		cache.put("deviceNetStatus",dev_no,obj,60);
	}

	public static String getDeviceGoodsStatus(String dev_no) {
		return cache.get("deviceGoodsStatus",dev_no);
	}

	public static void setDeviceGoodsStatus(String dev_Id, String obj) {
		
		if(obj==null){
			cache.remove("deviceGoodsStatus", dev_Id);
		}else{
		cache.put("deviceGoodsStatus",dev_Id,obj,4*60*60);
		}
	}
	
	
	public static String getTaoliangToken(String dev_no) {
		return cache.get("TaoliangToken",dev_no);
	}

	public static void setTaoliangToken(String dev_no, String obj) {
		
		if(obj==null){
			cache.remove("TaoliangToken", dev_no);
		}else{
		cache.put("TaoliangToken",dev_no,obj,3550);
		}
	}
	/**
	 * 获取设备实时统计数据
	 * @param
	 * @return
	 */
	public static String getDEVRealTimeXFCount(String imei) {
		return getRealTimeVal(imei+Key_xf);
		//return cache.get("RealTimeCount",imei+Key_xf);
	}
	
	public static String getDEVRealTimeOUTCount(String imei) {
		return getRealTimeVal(imei+Key_out);
	//	return cache.get("RealTimeCount",imei+Key_out);
	}
	/**
	 * 更新实时设备统计数据
	 * @param
	 * @return
	 */
	public static void setDEVRealTimeXFCount(String imei, String obj) {
			String key=imei+Key_xf;
			putRealTime(key,obj);
	}
	
	public static void setDEVRealTimeOUTCount(String imei, String obj) {
			String key=imei+Key_out;
			putRealTime(key,obj);
	}
	/**
	 * 实时gzh统计数据
	 * @param
	 * @return
	 */
	public static String getGZHRealTimeXFCount(String gzhno) {
		return getRealTimeVal(gzhno+Key_xf+"_gzh");
		//return cache.get("RealTimeCount",gzhno+Key_xf+"_gzh");
	}
	
	public static String getGZHRealTimeOUTCount(String gzhno) {
		return getRealTimeVal(gzhno+Key_out+"_gzh");
	//	return cache.get("RealTimeCount",gzhno+Key_out+"_gzh");
	}
	public static void setGZHRealTimeXFCount(String gzhno, String obj) {
		String key=gzhno+Key_xf+"_gzh";
		putRealTime(key,obj);
	}
	
	public static void setGZHRealTimeOUTCount(String gzhno, String obj) {
		String key=gzhno+Key_out+"_gzh";
		putRealTime(key,obj);
	}
	
	/**
	 * 实时task统计数据
	 * @param
	 * @return
	 */
	public static String getTaskRealTimeXFCount(String Taskno) {
		//System.out.println("get:"+Taskno+"///"+cache.get("RealTimeCount",Taskno+Key_xf+"_Task"));
		return getRealTimeVal(Taskno+Key_xf+"_Task");
	//	return cache.get("RealTimeCount",Taskno+Key_xf+"_Task");
	}
	
	public static String getTaskRealTimeOUTCount(String Taskno) {
		return getRealTimeVal(Taskno+Key_out+"_Task");
	//	return cache.get("RealTimeCount",Taskno+Key_out+"_Task");
	}
	public static void setTaskRealTimeXFCount(String Taskno, String obj) {
		//System.out.println("set:"+Taskno+"///"+obj);
		String key= Taskno+Key_xf+"_Task";
		putRealTime(key,obj);
	}
	
	public static void setTaskRealTimeOUTCount(String Taskno, String obj) {
		String key=Taskno+Key_out+"_Task";
		putRealTime(key,obj);
	}
	public static String getUserRealTimeXFCount(String userId) {
 
		return getRealTimeVal(userId+Key_xf+"_user");
	}

	public static void setUserRealTimeXFCount(String userId, String val) {
		String key=userId+Key_xf+"_user";
		putRealTime(key,val);
		
	}

	public static String getUserRealTimeOUTCount(String userId) {
 
		return getRealTimeVal(userId+Key_out+"_user");
	}

	public static void setUserRealTimeOUTCount(String userId, String val) {
		String key=userId+Key_out+"_user";
		putRealTime(key,val);
		
	}
	
	private final  static  String timeKey="_time";
	private final static String cachekey="RealTimeCount";
	private static String getRealTimeVal(String key){
		//String cachekey="RealTimeCount";
		if(cache.get(cachekey, key+timeKey)!=null){
			return cache.get(cachekey,key);
		}else{
			cache.remove(cachekey, key);
			return null;
		}
	}
	private  static void putRealTime(String key,String obj){
		//String cachekey="RealTimeCount";
		int time=60*60;
		if(obj==null){
			cache.remove(cachekey, key);
		}else{
			
			if(cache.get(cachekey,  key)!=null){ 
				time=0;
			}else{
				//缓存过期时间
				cache.put(cachekey, key+timeKey, "0", time);
			}
			//值
			cache.put(cachekey,key,obj,time);
		}
	}
	private final static String OUTPAPERMSG="RealTimeCount";
	private final static String cachek1="cachek1";
   
	
 
	public static Long orderincrease(String key) {
		// TODO Auto-generated method stub
		return cache.generateLongKey(key);
	}
	 public static void remove(String Cache ,String key){
		 cache.remove(Cache, key);
	 }
	 public static void remove(String key){
		 cache.remove(defultKey, key);
	 }
	 
	 public static void setMapValue(String mapkey, String   key,Object value){
		 System.out.println("set:"+mapkey+"_"+key);
		 CacheReBackMsg.memCacheImpl.remove(mapkey, key);
			Map map = (Map) CacheReBackMsg.get(mapkey );
			if (map == null) {
				map = new ConcurrentHashMap();
			}
			if (value != null) {
				
				map.put(key, value);
				CacheReBackMsg.put(mapkey, map);
			}
			
	 }

	
	public static Object getMapValue(String mapkey, String   key){
		 System.out.println("get:"+mapkey+"_"+key);
		 
		  
		 Map	 map= (Map) CacheReBackMsg.memCacheImpl.getObj(mapkey+key);
	 
		 if(map==null)    map = (Map) CacheReBackMsg.get(mapkey );
		 
		 if(map != null&&map.containsKey(key)){
			 CacheReBackMsg.memCacheImpl.setObj (mapkey+key,  map ,5);
			return map.get(key);
		 }
		 
		return null;
		 
	 }
	
	public static List<String> queryMapByStartKey(String mapkey, String   keyword){
		List <String>list=new ArrayList<String>();
		 Map map = (Map) CacheReBackMsg.get(mapkey);
		 System.out.println("queryMapByStartKey:"+map);
		 if(map==null)return list;
		 Iterator<Entry<Object, Rc>> it = map.entrySet().iterator();
			while (it.hasNext()) {
				Entry<Object, Rc> m= it.next();
				if(m.getKey().toString().startsWith(keyword)){
					list.add(m.getKey().toString());
				}
			}
			 System.out.println("queryMapByStartKey:"+list);
			return list;
	}
	
	public static void removeMapkey(String mapkey, String   key){
		Map map = (Map) CacheReBackMsg.get(mapkey );
		 if(map != null&&map.containsKey(key)){
				  map.remove(key);
			 }
			 
	}


 
}
